package com.pms.util;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * Created by hm on 2018/1/22.
 */
public class OSSSmsUtils {
    static final String product = "Dysmsapi";
    static final String domain = "dysmsapi.aliyuncs.com";

    static final String accessKeyId = "LTAIveMr8nLjAomt";
    static final String accessKeySecret = "p4MfZNsxyeFbEqBcdHYwkANDBM99Pf";

    /**
     * 阿里云短信模块
     * @param phone  电话
     * @param content   模板类型
     * @param code    验证码
     * @return
     */
    public static SendSmsResponse sendSms(String phone, String content, String code){
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "1000");
        System.setProperty("sun.net.client.defaultReadTimeout", "1000");
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        IAcsClient acsClient = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phone);
        request.setSignName("宏铭科技");
        request.setTemplateCode(content);
       JSONObject object =new JSONObject();
            object.put("code",code);
            request.setTemplateParam(object.toJSONString());
            SendSmsResponse sendSmsResponse = null;
            try {
                sendSmsResponse = acsClient.getAcsResponse(request);
            } catch (ClientException e) {
            e.printStackTrace();
        }
        return sendSmsResponse;
    }




}
